package com.ybkj.daijia.api.member.v3;

import com.ybkj.daijia.Setting;
import com.ybkj.daijia.serverUtils.SettingUtils;
import com.ybkj.daijia.api.beatpay.utils.BestPayCreateOrder;
import com.ybkj.daijia.api.beatpay.utils.BestPayOrderModel;
import com.ybkj.daijia.api.beatpay.utils.CryptTool;
import com.ybkj.daijia.api.member.ErrorConstant;
import com.ybkj.daijia.api.member.alipay.config.AlipayConfig;
import com.ybkj.daijia.api.member.alipay.sign.MemberOrderSign;
import com.ybkj.daijia.api.member.alipay.sign.RSA;
import com.ybkj.daijia.api.member.alipay.util.AlipayNotify;
import com.ybkj.daijia.api.member.transport.ResultTransport;
import com.ybkj.daijia.api.unionpay.AcpService;
import com.ybkj.daijia.api.unionpay.DemoBase;
import com.ybkj.daijia.api.unionpay.LogUtil;
import com.ybkj.daijia.api.unionpay.SDKConfig;
import com.ybkj.daijia.api.unionpay.SDKConstants;
import com.ybkj.daijia.api.weixin.tenpayv3.Configuration;
import com.ybkj.daijia.api.weixin.tenpayv3.GetWxOrderno;
import com.ybkj.daijia.api.weixin.tenpayv3.RequestHandler;
import com.ybkj.daijia.api.weixin.tenpayv3.Sha1Util;
import com.ybkj.daijia.api.weixin.tenpayv3.TenpayUtil;
import com.ybkj.daijia.server.event.model.ActivityCheckEvent;
import com.ybkj.daijia.server.event.model.MemberRechargeEvent;
import com.ybkj.daijia.server.mc.Passenger;
import com.ybkj.daijia.server.model.PayRecord;
import com.ybkj.daijia.server.model.PayRecord.PayRecordType;
import com.ybkj.daijia.server.reactor.ReactorVo;
import com.ybkj.daijia.server.sales.Activity.ActivityEvent;
import com.ybkj.daijia.server.service.MemberAccountService;
import com.ybkj.daijia.server.service.MemberService;
import com.ybkj.daijia.server.service.PayRecordService;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.net.URLEncoder;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.SortedMap;
import java.util.TreeMap;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang3.StringUtils;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.input.SAXBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.ApplicationEventMulticaster;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 支付相关
 *
 * @author mqs
 */
@Controller("MemberPayV3Controller")
@RequestMapping(value = "member/api/rest/v3/pay")
public class PayController {

    private static final Logger logger = LoggerFactory.getLogger(PayController.class);

    @Autowired
    private MemberAccountService memberAccountService;

    @Autowired
    private PayRecordService payRecordService;

    @Autowired
    private MemberService memberService;

    @Autowired
    private SettingUtils settingUtils;

    @Autowired
    private ApplicationEventMulticaster applicationEventMulticaster;

    public static Map<String, String> getAllRequestParam(final HttpServletRequest request) {
        Map<String, String> res = new HashMap<String, String>();
        Enumeration<?> temp = request.getParameterNames();
        if (null != temp) {
            while (temp.hasMoreElements()) {
                String en = (String) temp.nextElement();
                String value = request.getParameter(en);
                res.put(en, value);
                //在报文上送时，如果字段的值为空，则不上送<下面的处理为在获取所有参数数据时，判断若值为空，则删除这个字段>
                if (null == res.get(en) || "".equals(res.get(en))) {
                    res.remove(en);
                }
            }
        }
        return res;
    }

    /**
     * 支付宝发起支付.
     * $$$ 乘客支付接口
     */
    @RequestMapping(value = "alipay/prepay")
    public synchronized @ResponseBody
    ResultTransport alipayPrepay(HttpServletResponse response, HttpServletRequest request,
        String phone, String deviceType, BigDecimal money) {

        logger.debug("recv phone:{},deviceType:{},money:{}", phone, deviceType, money);

        if (StringUtils.isBlank(phone) || StringUtils.isBlank(deviceType) || null == money) {
            return ResultTransport.getErrorBy(ErrorConstant.PARAM_ERROR);
        }

        Passenger passenger = memberService.findOnePassengerByPhone(phone);
        if (null == passenger) {
            return ResultTransport.getErrorBy(ErrorConstant.MEMBER_NOT_EXISTS_ERROR);
        }

        try {

            Date now = new Date();
            SimpleDateFormat outFormat = new SimpleDateFormat("yyyyMMddHHmmss");
            String currTime = outFormat.format(now);
            String strTime = currTime.substring(8, currTime.length());
            String strRandom = TenpayUtil.buildRandom(8) + "";
            String tradeNO = strTime + strRandom;

            PayRecord payRecord = new PayRecord();
            payRecord.setAmount(money);
            payRecord.setDeviceType(deviceType);
            payRecord.setMemberId(passenger.getId());
            payRecord.setPhone(phone);
            payRecord.setTradeNO(tradeNO);
            payRecord.setPayRecordType(PayRecordType.alipay);

            payRecordService.inertRecord(payRecord);

            ResultTransport instance = ResultTransport.getErrorBy(ErrorConstant.SUCCESS);

            Setting setting = settingUtils.get();

            Map<String, Object> map = new HashMap<String, Object>();
            map.put("PartnerID", setting.getAlipayPartnerID());
            map.put("SellerID", setting.getAlipaySellerID());
            map.put("PartnerPrivKey", setting.getAlipayPartnerPrivKey());
//			map.put("PartnerPublicKey", setting.getAlipayPartnerPublicKey());
            map.put("PartnerPublicKey", AlipayConfig.ali_public_key);
            map.put("tradeNO", payRecord.getTradeNO());
            map.put("productName", "会员充值");
            map.put("productDescription", "会员充值");
//			map.put("amount", 0.01);
            map.put("amount", money);
            map.put("notifyURL", setting.getWebUrl() + "/member/api/rest/v3/pay/alipay/notify");

            String content = MemberOrderSign.sign(map);
            String sign = RSA.sign(content, setting.getAlipayPartnerPrivKey(), "utf-8");
            sign = URLEncoder.encode(sign, "utf-8");
            String url = content + "&sign=\"" + sign + "\"&sign_type=\"RSA\"";

            instance.setData(url);

            return instance;

        } catch (Exception e) {
            logger.error("api v3 pay alipay/prepay error: {}", e);
            return ResultTransport.getErrorBy(ErrorConstant.SERVER_ERROR);
        }
    }

    /**
     * 支付宝回调函数.
     */
    @RequestMapping(value = "alipay/notify")
    public synchronized @ResponseBody
    String alipayNotify(HttpServletResponse response, HttpServletRequest request)
        throws UnsupportedEncodingException {
        logger.debug("alipay/notify---------------");

        Setting setting = settingUtils.get();

        //获取支付宝POST过来反馈信息
        Map<String, String> params = new HashMap<String, String>();
        Map requestParams = request.getParameterMap();
        for (Iterator iter = requestParams.keySet().iterator(); iter.hasNext(); ) {
            String name = (String) iter.next();
            String[] values = (String[]) requestParams.get(name);
            String valueStr = "";
            for (int i = 0; i < values.length; i++) {
                valueStr = (i == values.length - 1) ? valueStr + values[i]
                    : valueStr + values[i] + ",";
            }
            //乱码解决，这段代码在出现乱码时使用。如果mysign和sign不相等也可以使用这段代码转化
            //valueStr = new String(valueStr.getBytes("ISO-8859-1"), "gbk");
            params.put(name, valueStr);
        }

        //获取支付宝的通知返回参数，可参考技术文档中页面跳转同步通知参数列表(以下仅供参考)//

        //商户订单号
        String out_trade_no = new String(
            request.getParameter("out_trade_no").getBytes("ISO-8859-1"), "UTF-8");

        //支付宝交易号
        String trade_no = new String(request.getParameter("trade_no").getBytes("ISO-8859-1"),
            "UTF-8");

        //交易状态
        String trade_status = new String(
            request.getParameter("trade_status").getBytes("ISO-8859-1"), "UTF-8");

        //获取支付宝的通知返回参数，可参考技术文档中页面跳转同步通知参数列表(以上仅供参考)//

        logger.debug("商户订单号：" + out_trade_no);
        logger.debug("交易号：" + trade_no);
        logger.debug("交易状态：" + trade_status);

        if (AlipayNotify.verify(params, setting)) {//验证成功
            //请在这里加上商户的业务逻辑程序代码

            if (trade_status.equals("TRADE_FINISHED")) {
                //判断该笔订单是否在商户网站中已经做过处理
                //如果没有做过处理，根据订单号（out_trade_no）在商户网站的订单系统中查到该笔订单的详细，并执行商户的业务程序
                //如果有做过处理，不执行商户的业务程序

                //注意：
                //该种交易状态只在两种情况下出现
                //1、开通了普通即时到账，买家付款成功后。
                //2、开通了高级即时到账，从该笔交易成功时间算起，过了签约时的可退款时限（如：三个月以内可退款、一年以内可退款等）后。
                PayRecord payRecord = payRecordService.findByTradeNO(out_trade_no);
                if (null != payRecord && !payRecord.isTreatment()) {

                    try {
                        memberService.cashRecharge(payRecord, trade_no);

                        Passenger passenger = memberService
                            .findOnePassenger(payRecord.getMemberId());

                        if (null != passenger) {
                            ReactorVo rVo = new ReactorVo();
                            rVo.setPassenger(passenger);
                            rVo.setMoney(payRecord.getAmount());
                            rVo.setType("alipay");
                            MemberRechargeEvent rechargeEvent = new MemberRechargeEvent(rVo);
                            applicationEventMulticaster.multicastEvent(rechargeEvent);

                            ReactorVo rVo2 = new ReactorVo();
                            rVo2.setPassenger(passenger);
                            rVo2.setMoney(payRecord.getAmount());
                            rVo2.setActivityEvent(ActivityEvent.recharging);
                            ActivityCheckEvent activityCheckEvent = new ActivityCheckEvent(rVo2);
                            applicationEventMulticaster.multicastEvent(activityCheckEvent);
                        }

                        logger.debug("success-----------------");
                        return "success";
                    } catch (Exception e) {
                        logger.debug("fail：" + e.getMessage());
                        e.printStackTrace();
                    }

                } else {
                    logger.debug("订单：" + out_trade_no + "不存在或已完成");
                }


            } else if (trade_status.equals("TRADE_SUCCESS")) {
                //判断该笔订单是否在商户网站中已经做过处理
                //如果没有做过处理，根据订单号（out_trade_no）在商户网站的订单系统中查到该笔订单的详细，并执行商户的业务程序
                //如果有做过处理，不执行商户的业务程序

                //注意：
                //该种交易状态只在一种情况下出现——开通了高级即时到账，买家付款成功后。
                PayRecord payRecord = payRecordService.findByTradeNO(out_trade_no);
                if (null != payRecord && !payRecord.isTreatment()) {
                    try {
                        memberService.cashRecharge(payRecord, trade_no);

                        Passenger passenger = memberService
                            .findOnePassenger(payRecord.getMemberId());

                        if (null != passenger) {
                            ReactorVo rVo = new ReactorVo();
                            rVo.setPassenger(passenger);
                            rVo.setMoney(payRecord.getAmount());
                            rVo.setType("alipay");
                            MemberRechargeEvent rechargeEvent = new MemberRechargeEvent(rVo);
                            applicationEventMulticaster.multicastEvent(rechargeEvent);

                            ReactorVo rVo2 = new ReactorVo();
                            rVo2.setPassenger(passenger);
                            rVo2.setMoney(payRecord.getAmount());
                            rVo2.setActivityEvent(ActivityEvent.recharging);
                            ActivityCheckEvent activityCheckEvent = new ActivityCheckEvent(rVo2);
                            applicationEventMulticaster.multicastEvent(activityCheckEvent);


                        }

                        logger.debug("success-----------------");
                        return "success";
                    } catch (Exception e) {
                        logger.debug("fail：" + e.getMessage());
                        e.printStackTrace();
                    }
                } else {
                    logger.debug("订单：" + out_trade_no + "不存在或已完成");
                }
            }

            logger.debug("fail-----------------");
            return "fail";

        } else {
            logger.debug("验证失败-----------------");
            return "fail";
        }
    }

    /**
     * 微信发起支付.
     * $$$ 乘客微信支付接口
     */
    @RequestMapping(value = "wx/prepay")
    public synchronized @ResponseBody
    ResultTransport wxAfterpay(HttpServletResponse response, HttpServletRequest request,
        String phone, String deviceType, BigDecimal money) {

        logger.debug("recv phone:{},deviceType:{},money:{}", phone, deviceType, money);

        if (StringUtils.isBlank(phone) || StringUtils.isBlank(deviceType) || null == money) {
            return ResultTransport.getErrorBy(ErrorConstant.PARAM_ERROR);
        }

        Passenger passenger = memberService.findOnePassengerByPhone(phone);
        if (null == passenger) {
            return ResultTransport.getErrorBy(ErrorConstant.MEMBER_NOT_EXISTS_ERROR);
        }

        //获取openId后调用统一支付接口https://api.mch.weixin.qq.com/pay/unifiedorder
        String currTime = TenpayUtil.getCurrTime();
        //8位日期
        String strTime = currTime.substring(8, currTime.length());
        //四位随机数
        String strRandom = TenpayUtil.buildRandom(6) + "";
        //10位序列号,可以自行调整。
        String strReq = strTime + strRandom;

        PayRecord payRecord = new PayRecord();
        payRecord.setAmount(money);
        payRecord.setDeviceType(deviceType);
        payRecord.setMemberId(passenger.getId());
        payRecord.setPhone(phone);
        payRecord.setTradeNO(strReq);
        payRecord.setPayRecordType(PayRecordType.wx);

        payRecordService.inertRecord(payRecord);

        ResultTransport instance = ResultTransport.getErrorBy(ErrorConstant.SUCCESS);

        Setting setting = settingUtils.get();
        String createOrderURL = Configuration.createOrderURL;

        //商户相关资料
        String appid = setting.getWxAppID();
        String appsecret = setting.getWxAppSecret();
        String partner = setting.getWxPartnerID();
        String partnerkey = setting.getWxPartnerKey();

        //商户号
        String mch_id = partner;
        //随机数
        String nonce_str = strReq;
        //商品描述
        String body = "会员充值";
        //商户订单号
        String out_trade_no = payRecord.getTradeNO();
        //总金额以分为单位，不带小数点
        String total_fee = String.valueOf(money.multiply(new BigDecimal(100)).intValue());
        //订单生成的机器 IP
        String spbill_create_ip = request.getRemoteAddr();

        //这里notify_url是 支付完成后微信发给该链接信息，可以判断会员是否支付成功，改变订单状态等。
        String notify_url = setting.getWebUrl() + "/member/api/rest/v3/pay/wx/notify";

        String trade_type = "APP";

        SortedMap<String, String> packageParams = new TreeMap<String, String>();
        packageParams.put("appid", appid);
        packageParams.put("mch_id", mch_id);
        packageParams.put("nonce_str", nonce_str);
        packageParams.put("body", body);
        packageParams.put("out_trade_no", out_trade_no);

        //这里写的金额为1 分到时修改
//			packageParams.put("total_fee", "1");
        packageParams.put("total_fee", total_fee);
        packageParams.put("spbill_create_ip", spbill_create_ip);
        packageParams.put("notify_url", notify_url);
        packageParams.put("trade_type", trade_type);

        RequestHandler reqHandler = new RequestHandler(request, response);
        reqHandler.init(appid, appsecret, partnerkey);

        String sign = reqHandler.createSign(packageParams);
        String xml = "<xml>" +
            "<appid>" + appid + "</appid>" +
            "<mch_id>" + mch_id + "</mch_id>" +
            "<nonce_str>" + nonce_str + "</nonce_str>" +
            "<sign>" + sign + "</sign>" +
            "<body><![CDATA[" + body + "]]></body>" +
            "<out_trade_no>" + out_trade_no + "</out_trade_no>" +
//					"<total_fee>"+1+"</total_fee>"+
            "<total_fee>" + total_fee + "</total_fee>" +
            "<spbill_create_ip>" + spbill_create_ip + "</spbill_create_ip>" +
            "<notify_url>" + notify_url + "</notify_url>" +
            "<trade_type>" + trade_type + "</trade_type>" +
            "</xml>";

        String prepay_id = "";
        try {
            prepay_id = GetWxOrderno.getPayNo(createOrderURL, xml);
            if (prepay_id.equals("")) {
                logger.error("统一支付接口获取预支付订单出错");
            }
        } catch (Exception e1) {
            e1.printStackTrace();
        }
        SortedMap<String, String> finalpackage = new TreeMap<String, String>();
        String timestamp = Sha1Util.getTimeStamp();
        finalpackage.put("appid", appid);
        finalpackage.put("partnerid", partner);
        finalpackage.put("prepayid", prepay_id);
        finalpackage.put("package", "Sign=WXPay");
        finalpackage.put("noncestr", nonce_str);
        finalpackage.put("timestamp", timestamp);
        String finalsign = reqHandler.createSign(finalpackage);
        finalpackage.put("sign", finalsign);

        instance.setData(finalpackage);
        return instance;

    }

    /**
     * 微信支付后接口.
     */
    @RequestMapping("wx/notify")
    public synchronized @ResponseBody
    String wxAfterNotify(HttpServletResponse response, HttpServletRequest request)
        throws Exception {

        String out_trade_no = "";

        String trade_no = "";

        try {

            BufferedReader br = new BufferedReader(new InputStreamReader(request.getInputStream()));
            String line = null;
            StringBuilder sb = new StringBuilder();
            while ((line = br.readLine()) != null) {
                sb.append(line);
            }
            String strxml = sb.toString();

            InputStream in = new ByteArrayInputStream(strxml.getBytes());
            SAXBuilder builder = new SAXBuilder();
            Document doc = builder.build(in);
            Element root = doc.getRootElement();
            List list = root.getChildren();
            Iterator it = list.iterator();
            while (it.hasNext()) {
                Element e = (Element) it.next();
                String k = e.getName();
                if (k.equals("out_trade_no")) {
                    out_trade_no = e.getText();
                } else if (k.equals("transaction_id")) {
                    trade_no = e.getText();
                }
            }

            try {
                //关闭流
                if (null != in) {
                    in.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

            if (StringUtils.isBlank(out_trade_no)) {
                return "FAIL";
            }

            PayRecord payRecord = payRecordService.findByTradeNO(out_trade_no);

            if (null == payRecord) {
                return "FAIL";
            }

            if (payRecord.isTreatment()) {
                return "SUCCESS";
            }

            try {
                memberService.cashRecharge(payRecord, trade_no);

                Passenger passenger = memberService.findOnePassenger(payRecord.getMemberId());

                if (null != passenger) {
                    ReactorVo rVo = new ReactorVo();
                    rVo.setPassenger(passenger);
                    rVo.setMoney(payRecord.getAmount());
                    rVo.setType("weixin");
                    MemberRechargeEvent rechargeEvent = new MemberRechargeEvent(rVo);
                    applicationEventMulticaster.multicastEvent(rechargeEvent);

                    ReactorVo rVo2 = new ReactorVo();
                    rVo2.setPassenger(passenger);
                    rVo2.setMoney(payRecord.getAmount());
                    rVo2.setActivityEvent(ActivityEvent.recharging);
                    ActivityCheckEvent activityCheckEvent = new ActivityCheckEvent(rVo2);
                    applicationEventMulticaster.multicastEvent(activityCheckEvent);
                }

                return "SUCCESS";
            } catch (Exception e) {
                e.printStackTrace();
                return "FAIL";
            }


        } catch (Exception e) {
            e.printStackTrace();
            return "FAIL";
        }

    }

    /**
     * 银联支付发起.
     */
    @RequestMapping(value = "unionpay/prepay")
    public @ResponseBody
    ResultTransport unionpayPreDoPost(
        HttpServletResponse resp, HttpServletRequest req,
        String phone, String deviceType, BigDecimal money) throws IOException {
        logger.debug("recv phone:{},deviceType:{},money:{}", phone, deviceType, money);

        if (StringUtils.isBlank(phone) || StringUtils.isBlank(deviceType) || null == money) {
            return ResultTransport.getErrorBy(ErrorConstant.PARAM_ERROR);
        }

        Passenger passenger = memberService.findOnePassengerByPhone(phone);
        if (null == passenger) {
            return ResultTransport.getErrorBy(ErrorConstant.MEMBER_NOT_EXISTS_ERROR);
        }
        String orderId = DemoBase.getOrderId();

        PayRecord payRecord = new PayRecord();
        payRecord.setAmount(money);
        payRecord.setDeviceType(deviceType);
        payRecord.setMemberId(passenger.getId());
        payRecord.setPhone(phone);
        payRecord.setTradeNO(orderId);
        payRecord.setPayRecordType(PayRecordType.unionpay);

        payRecordService.inertRecord(payRecord);

        // 从classpath加载acp_sdk.properties文件
        Setting setting = settingUtils.get();
        SDKConfig.getConfig().loadPropertiesFromSrc(setting);

        int payMoney = money.multiply(new BigDecimal(100))
            .intValue();  //txnAmt以'元'为单位,乘以100以'分'为单位传给银联
        String txnTime = DemoBase.getCurrentTime(); //交易时间
        String merId = setting.getMerId();  //获取商户号
        Map<String, String> contentData = new HashMap<String, String>();

        /***银联全渠道系统，产品参数，除了encoding自行选择外其他不需修改***/
        contentData.put("version", "5.1.0");            //版本号 全渠道默认值
        contentData.put("encoding", "UTF-8");     //字符集编码 可以使用UTF-8,GBK两种方式
        contentData.put("signMethod", "01"); //签名方法
        contentData.put("txnType", "01");                       //交易类型 01:消费
        contentData.put("txnSubType", "01");                    //交易子类 01：消费
        contentData.put("bizType", "000201");                   //填写000201
        contentData.put("channelType", "08");                   //渠道类型 08手机

        /***商户接入参数***/
        contentData
            .put("merId", merId);                        //商户号码，请改成自己申请的商户号或者open上注册得来的777商户号测试
        contentData
            .put("accessType", "0");                     //接入类型，商户接入填0 ，不需修改（0：直连商户， 1： 收单机构 2：平台商户）
        contentData
            .put("orderId", orderId);                    //商户订单号，8-40位数字字母，不能含“-”或“_”，可以自行定制规则
        contentData.put("txnTime",
            txnTime);                    //订单发送时间，取系统时间，格式为YYYYMMDDhhmmss，必须取当前时间，否则会报txnTime无效
        contentData.put("accType", "01");                       //账号类型 01：银行卡02：存折03：IC卡帐号类型(卡介质)
        contentData.put("txnAmt", String.valueOf(payMoney));                      //交易金额 单位为分，不能带小数点
        contentData.put("currencyCode", "156");                 //境内商户固定 156 人民币

        // 请求方保留域，
        // 透传字段，查询、通知、对账文件中均会原样出现，如有需要请启用并修改自己希望透传的数据。
        // 出现部分特殊字符时可能影响解析，请按下面建议的方式填写：
        // 1. 如果能确定内容不会出现&={}[]"'等符号时，可以直接填写数据，建议的方法如下。
        // 2. 内容可能出现&={}[]"'符号时：
        // 1) 如果需要对账文件里能显示，可将字符替换成全角＆＝｛｝【】“‘字符（自己写代码，此处不演示）；
        // 2) 如果对账文件没有显示要求，可做一下base64（如下）。
        //    注意控制数据长度，实际传输的数据长度不能超过1024位。
        //    查询、通知等接口解析时使用new String(Base64.decodeBase64(reqReserved), DemoBase.encoding);解base64后再对数据做后续解析。
//      contentData.put("reqReserved", Base64.encodeBase64String(out_trade_no.toString().getBytes("UTF-8")));
        contentData.put("reqReserved", orderId);
        //后台通知地址（需设置为外网能访问 http https均可），支付成功后银联会自动将异步通知报文post到商户上送的该地址，【支付失败的交易银联不会发送后台通知】
        //后台通知参数详见open.unionpay.com帮助中心 下载  产品接口规范  网关支付产品接口规范 消费交易 商户通知
        //注意:1.需设置为外网能访问，否则收不到通知    2.http https均可  3.收单后台通知后需要10秒内返回http200或302状态码
        //    4.如果银联通知服务器发送通知后10秒内未收到返回状态码或者应答码非http200或302，那么银联会间隔一段时间再次发送。总共发送5次，银联后续间隔1、2、4、5 分钟后会再次通知。
        //    5.后台通知地址如果上送了带有？的参数，例如：http://abc/web?a=b&c=d 在后台通知处理程序验证签名之前需要编写逻辑将这些字段去掉再验签，否则将会验签失败
        String backUrl = "";
        if (setting.getWebUrl().endsWith("/")) {
            backUrl = setting.getWebUrl() + "member/api/rest/v3/pay/unionpay/notify";
        } else {
            backUrl = setting.getWebUrl() + "/" + "member/api/rest/v3/pay/unionpay/notify";
        }
        contentData.put("backUrl", backUrl);

        /**对请求参数进行签名并发送http post请求，接收同步应答报文**/
        Map<String, String> reqData = AcpService.sign(contentData,
            DemoBase.encoding);            //报文中certId,signature的值是在signData方法中获取并自动赋值的，只要证书配置正确即可。
        String requestAppUrl = SDKConfig.getConfig()
            .getAppRequestUrl();                                 //交易请求url从配置文件读取对应属性文件acp_sdk.properties中的 acpsdk.backTransUrl
        Map<String, String> rspData = AcpService.post(reqData, requestAppUrl,
            DemoBase.encoding);  //发送请求报文并接受同步应答（默认连接超时时间30秒，读取返回结果超时时间30秒）;这里调用signData之后，调用submitUrl之前不能对submitFromData中的键值对做任何修改，如果修改会导致验签不通过

        /**对应答码的处理，请根据您的业务逻辑来编写程序,以下应答码处理逻辑仅供参考------------->**/
        //应答码规范参考open.unionpay.com帮助中心 下载  产品接口规范  《平台接入接口规范-第5部分-附录》
        if (!rspData.isEmpty()) {
            if (AcpService.validate(rspData, DemoBase.encoding)) {
                LogUtil.writeLog("验证签名成功");
                String respCode = rspData.get("respCode");
                if (("00").equals(respCode)) {
                    //成功,获取tn交易流水号
                    String tn = rspData.get("tn");
                    ResultTransport transport = ResultTransport.getErrorBy(ErrorConstant.SUCCESS);
                    transport.setData(tn);
                    return transport;
                } else {
                    //其他应答码为失败请排查原因或做失败处理
                    return ResultTransport.getErrorBy(ErrorConstant.SERVER_ERROR);
                }
            } else {
                LogUtil.writeErrorLog("验证签名失败");
                return ResultTransport.getErrorBy(ErrorConstant.SERVER_ERROR);
            }
        } else {
            //未返回正确的http状态
            LogUtil.writeErrorLog("未获取到返回报文或返回http状态码非200");
            return ResultTransport.getErrorBy(ErrorConstant.SERVER_ERROR);
        }
    }

    /**
     * 银联支付回调.
     */
    @RequestMapping(value = "unionpay/notify")
    public synchronized @ResponseBody
    void unionpayNotify(
        HttpServletResponse resp, HttpServletRequest req) throws Exception {

        LogUtil.writeLog("BackRcvResponse接收后台通知开始");

        String encoding = req.getParameter(SDKConstants.param_encoding);
        // 获取银联通知服务器发送的后台通知参数
        Map<String, String> reqParam = getAllRequestParam(req);

        LogUtil.printRequestLog(reqParam);

        Map<String, String> valideData = null;
        if (null != reqParam && !reqParam.isEmpty()) {
            Iterator<Entry<String, String>> it = reqParam.entrySet().iterator();
            valideData = new HashMap<String, String>(reqParam.size());
            while (it.hasNext()) {
                Entry<String, String> e = it.next();
                String key = (String) e.getKey();
                String value = (String) e.getValue();

                valideData.put(key, value);
            }
        }

        //重要！验证签名前不要修改reqParam中的键值对的内容，否则会验签不过
        if (!AcpService.validate(valideData, encoding)) {
            LogUtil.writeLog("验证签名结果[失败].");
            //验签失败，需解决验签问题
        } else {
            LogUtil.writeLog("验证签名结果[成功].");
            //【注：为了安全验签成功才应该写商户的成功处理逻辑】交易成功，更新商户订单状态
            String orderId = valideData.get("orderId");
            LogUtil.writeLog("后台通知的数据orderId:" + orderId);
            String respCode = valideData.get("respCode");

            if ("00".equals(respCode)) {
                String out_trade_no = valideData.get("reqReserved");  //获取后台通知的数据，其他字段也可用类似方式获取
                String txnAmt = valideData.get("txnAmt");

                //判断respCode=00、A6后，对涉及资金类的交易，请再发起查询接口查询，确定交易成功后更新数据库。

                //判断该笔订单是否在商户网站中已经做过处理
                //如果没有做过处理，根据订单号（out_trade_no）在商户网站的订单系统中查到该笔订单的详细，并执行商户的业务程序
                //如果有做过处理，不执行商户的业务程序

                //注意：
                //该种交易状态只在一种情况下出现——开通了高级即时到账，买家付款成功后。
                PayRecord payRecord = payRecordService.findByTradeNO(out_trade_no);
                if (null != payRecord && !payRecord.isTreatment()) {
                    try {
                        memberService.cashRecharge(payRecord, out_trade_no);

                        Passenger passenger = memberService
                            .findOnePassenger(payRecord.getMemberId());

                        if (null != passenger) {
                            ReactorVo rVo = new ReactorVo();
                            rVo.setPassenger(passenger);
                            rVo.setMoney(payRecord.getAmount());
                            rVo.setType("unionpay");
                            MemberRechargeEvent rechargeEvent = new MemberRechargeEvent(rVo);
                            applicationEventMulticaster.multicastEvent(rechargeEvent);

                            ReactorVo rVo2 = new ReactorVo();
                            rVo2.setPassenger(passenger);
                            rVo2.setMoney(payRecord.getAmount());
                            rVo2.setActivityEvent(ActivityEvent.recharging);
                            ActivityCheckEvent activityCheckEvent = new ActivityCheckEvent(rVo2);
                            applicationEventMulticaster.multicastEvent(activityCheckEvent);

                        }
                        logger.debug("success-----------------");
                    } catch (Exception e) {
                        logger.debug("fail：" + e.getMessage());
                        e.printStackTrace();
                    }
                } else {
                    logger.debug("订单：" + out_trade_no + "不存在或已完成");
                }


            }

        }
        LogUtil.writeLog("BackRcvResponse接收后台通知结束");
        //返回给银联服务器http 200  状态码
        resp.getWriter().print("ok");
    }

    /***********************************************************************************
     *
     * 翼支付
     *
     ***********************************************************************************/

    /**
     * 翼支付发起预支付
     *
     * @param request
     * @param phone      客户电话
     * @param deviceType 设备终端类型
     * @param money      充值金额
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping("bestpay/prepay")
    public synchronized ResultTransport bestPayByPre(HttpServletRequest request, String phone,
        String deviceType, BigDecimal money) throws Exception {

        ResultTransport transport = null;

        // 验证参数
        if (StringUtils.isBlank(phone)
            || StringUtils.isBlank(deviceType)
            || null == money) {
            transport = ResultTransport.getErrorBy(ErrorConstant.PARAM_ERROR);
            return transport;
        }

        // 格式化金额0.00
        DecimalFormat myformat = new java.text.DecimalFormat("0.00");
        String money_ = myformat.format(money);

        // 获取用户
        Passenger passenger = memberService.findOnePassengerByPhone(phone);
        if (null == passenger) {
            return ResultTransport.getErrorBy(ErrorConstant.MEMBER_NOT_EXISTS_ERROR);
        }

        String currTime = TenpayUtil.getCurrTime();
        String strTime = currTime.substring(8, currTime.length()); //8位日期
        String strRandom = TenpayUtil.buildRandom(6) + ""; //四位随机数
        String strReq = strTime + strRandom; // 10位序列号,可以自行调整

        // 插入客户充值临时记录
        PayRecord payRecord = new PayRecord();
        payRecord.setAmount(money);
        payRecord.setDeviceType(deviceType);
        payRecord.setMemberId(passenger.getId());
        payRecord.setPhone(phone);
        payRecord.setTradeNO(strReq);
        payRecord.setPayRecordType(PayRecordType.bestpay);
        payRecordService.inertRecord(payRecord);

        // 获取系统设置
        Setting setting = settingUtils.get();
        String callBackUrl = setting.getWebUrl() + "/member/api/rest/v3/pay/bestpay/notify";
        String createUrl = "https://webpaywg.bestpay.com.cn/order.action";

        // 获取商户信息
        String mch_id = setting.getMerchantId();
        String mch_key = setting.getMerKey();
        String mch_pwd = setting.getMchPwd();
        String riskInfo = setting.getRiskcontrolinfo();
        String body = "客户充值";

        // 订单模型
        String total_fee = String.valueOf(money.multiply(new BigDecimal(100)).intValue()); // 元转分
        BestPayOrderModel orderModel = new BestPayOrderModel();
        orderModel.setATTACH("null");
        orderModel.setCITYCODE("");
        orderModel.setDIVDETAILS("");
        orderModel.setENCODE("");
        orderModel.setENCODETYPE("1");
        orderModel.setKEY(mch_key);
        orderModel.setLOGINNO("");
        orderModel.setMERCHANTID(mch_id);
        orderModel.setORDERAMT(total_fee); // 分
        orderModel.setORDERCCY("RMB");
        orderModel.setORDERREQTIME(currTime);
        orderModel.setORDERREQTRANSEQ(strReq);
        orderModel.setORDERSEQ(strReq);
        orderModel.setPRODUCTDESC(body);
        orderModel.setPRODUCTID("04");
        orderModel.setPROVINCECODE("");
        orderModel.setRISKCONTROLINFO(riskInfo);
        orderModel.setSERVICECODE("05");
        orderModel.setSESSIONKEY("");
        orderModel.setSUBMERCHANTID("");
        orderModel.setTRANSCODE("01");
        boolean rs = BestPayCreateOrder.createByApp(createUrl, orderModel);

        // 下单失败
        if (!rs) {
            transport = ResultTransport.getErrorBy(ErrorConstant.BESTPAY_APP_CREATE_ORDER_ERR);
            return transport;
        }

        // 签名参数
        StringBuffer md5Buffer = new StringBuffer();
        md5Buffer.append("SERVICE=").append("mobile.security.pay")
            .append("&MERCHANTID=").append(mch_id)
            .append("&MERCHANTPWD=").append(mch_pwd)
            .append("&SUBMERCHANTID=").append("")
            .append("&BACKMERCHANTURL=").append(callBackUrl)
            .append("&ORDERSEQ=").append(strReq)
            .append("&ORDERREQTRANSEQ=").append(strReq)
            .append("&ORDERTIME=").append(currTime)
            .append("&ORDERVALIDITYTIME=").append("")
            .append("&CURTYPE=").append("RMB")
            .append("&ORDERAMOUNT=").append(money_)
            .append("&SUBJECT=").append(body)
            .append("&PRODUCTID=").append("04")
            .append("&PRODUCTDESC=").append(body)
            .append("&CUSTOMERID=").append(phone)
            .append("&SWTICHACC=").append("true")
            .append("&KEY=").append(mch_key);

        // 签名
        logger.info("MAC(MD5)--before--String：" + md5Buffer.toString());
        String sign = CryptTool.md5Digest(md5Buffer.toString());
        logger.info("MAC(MD5)--after--String:" + sign);

        // 拼接返回数据串
        StringBuffer paramsBuffer = new StringBuffer();
        paramsBuffer.append("SERVICE=").append("mobile.security.pay")
            .append("&MERCHANTID=").append(mch_id) // 签约商户号
            .append("&MERCHANTPWD=").append(mch_pwd)
            .append("&SUBMERCHANTID=").append("")
            .append("&BACKMERCHANTURL=").append(callBackUrl)
            .append("&SIGNTYPE=").append("MD5")
            .append("&SIGN=").append(sign)
            .append("&ORDERSEQ=").append(strReq)
            .append("&ORDERREQTRANSEQ=").append(strReq)
            .append("&ORDERTIME=").append(currTime)
            .append("&ORDERVALIDITYTIME=").append("")
            .append("&ORDERAMOUNT=").append(money_)
            .append("&CURTYPE=").append("RMB")
            .append("&PRODUCTID=").append("04")
            .append("&PRODUCTDESC=").append(body)
            .append("&PRODUCTAMOUNT=").append(money_)
            .append("&ATTACHAMOUNT=").append("0.00")
            .append("&ATTACH=").append("")
            .append("&DIVDETAILS=").append("")
            .append("&ACCOUNTID=").append("")
            .append("&CUSTOMERID=").append(phone)
            .append("&USERIP=").append("")
            .append("&BUSITYPE=").append("04")
            .append("&SWTICHACC=").append("true")
            .append("&SUBJECT=").append(body);

        // 返回数据
        String params = paramsBuffer.toString();
        transport = ResultTransport.getErrorBy(ErrorConstant.SUCCESS);
        transport.setData(params);
        return transport;
    }

    /**
     * 翼支付支付信息回调接口
     *
     * @param request
     * @throws Exception
     */
    @RequestMapping("bestpay/notify")
    public synchronized void bestPayNotify(HttpServletRequest request,
        HttpServletResponse response) {
        try {
            // 准备
            request.setCharacterEncoding("UTF-8");
            response.setCharacterEncoding("UTF-8");
            PrintWriter out = response.getWriter();

            // 获取回调数据，注释部分为没有使用的部分数据，其所代表的意义详见翼支付APP文档
            String UPTRANSEQ = request.getParameter("UPTRANSEQ");
            String MERCHANTID = request.getParameter("MERCHANTID");
            String TRANDATE = request.getParameter("TRANDATE");
            String RETNCODE = request.getParameter("RETNCODE");
            String RETNINFO = request.getParameter("RETNINFO");
//			String ORDERREQTRANSEQ = request.getParameter("ORDERREQTRANSEQ");
            String ORDERSEQ = request.getParameter("ORDERSEQ");
            String ORDERAMOUNT = request.getParameter("ORDERAMOUNT");
//			String PRODUCTAMOUNT = request.getParameter("PRODUCTAMOUNT");
//			String ATTACHAMOUNT = request.getParameter("ATTACHAMOUNT");
//			String CURTYPE = request.getParameter("CURTYPE");
//			String ENCODETYPE = request.getParameter("ENCODETYPE");
//			String BANKID = request.getParameter("BANKID");
//			String ATTACH = request.getParameter("ATTACH");
//			String UPREQTRANSEQ = request.getParameter("UPREQTRANSEQ");
//			String UPBANKTRANSEQ = request.getParameter("UPBANKTRANSEQ");
//			String PRODUCTNO = request.getParameter("PRODUCTNO");
            String SIGN = request.getParameter("SIGN");

            // 验证签名
            Setting setting = settingUtils.get();
            String key = setting.getMerKey();
            String signStr = "UPTRANSEQ=" + UPTRANSEQ
                + "&MERCHANTID=" + MERCHANTID
                + "&ORDERSEQ=" + ORDERSEQ
                + "&ORDERAMOUNT=" + ORDERAMOUNT
                + "&RETNCODE=" + RETNCODE
                + "&RETNINFO=" + RETNINFO
                + "&TRANDATE=" + TRANDATE
                + "&KEY=" + key;
            String localSign = CryptTool.md5Digest(signStr);

            // 验签失败
            if (!localSign.equals(SIGN)) {
                out.write("UPTRANSEQ_" + UPTRANSEQ);
                logger.info("支付回调，验签失败！");
                return;
            }

            // 验签成功，看是否支付成功("0000为成功，其他均为失败")
            if (RETNCODE.equals("0000")) {
                PayRecord payRecord = payRecordService.findByTradeNO(ORDERSEQ);

                if (null == payRecord) {
                    logger.info("没有该订单记录");
                    out.write("UPTRANSEQ_" + UPTRANSEQ);
                    return;
                }

                if (payRecord.isTreatment()) {
                    logger.info("该订单记录已完成");
                    out.write("UPTRANSEQ_" + UPTRANSEQ);
                    return;
                }

                memberService.cashRecharge(payRecord, UPTRANSEQ);
                Passenger passenger = memberService.findOnePassenger(payRecord.getMemberId());
                if (null != passenger) {
                    ReactorVo rVo = new ReactorVo();
                    rVo.setPassenger(passenger);
                    rVo.setMoney(payRecord.getAmount());
                    rVo.setType("bestpay");
                    MemberRechargeEvent rechargeEvent = new MemberRechargeEvent(rVo);
                    applicationEventMulticaster.multicastEvent(rechargeEvent);

                    ReactorVo rVo2 = new ReactorVo();
                    rVo2.setPassenger(passenger);
                    rVo2.setMoney(payRecord.getAmount());
                    rVo2.setActivityEvent(ActivityEvent.recharging);
                    ActivityCheckEvent activityCheckEvent = new ActivityCheckEvent(rVo2);
                    applicationEventMulticaster.multicastEvent(activityCheckEvent);
                }
            }
            out.write("UPTRANSEQ_" + UPTRANSEQ);
        } catch (Exception e) {
            // 异常不返回，以便不丢失支付数据
            e.printStackTrace();
        }
    }
}
